/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador;

import controlador.exceptions.IllegalOrphanException;
import controlador.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entidad.Proveedores;
import entidad.Lotes;
import entidad.Ordenentrada;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author a
 */
public class OrdenentradaJpaController implements Serializable {

    public OrdenentradaJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Ordenentrada ordenentrada) {
        if (ordenentrada.getLotesCollection() == null) {
            ordenentrada.setLotesCollection(new ArrayList<Lotes>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Proveedores proveedoridProveedor = ordenentrada.getProveedoridProveedor();
            if (proveedoridProveedor != null) {
                proveedoridProveedor = em.getReference(proveedoridProveedor.getClass(), proveedoridProveedor.getIdProveedores());
                ordenentrada.setProveedoridProveedor(proveedoridProveedor);
            }
            Collection<Lotes> attachedLotesCollection = new ArrayList<Lotes>();
            for (Lotes lotesCollectionLotesToAttach : ordenentrada.getLotesCollection()) {
                lotesCollectionLotesToAttach = em.getReference(lotesCollectionLotesToAttach.getClass(), lotesCollectionLotesToAttach.getIdLotes());
                attachedLotesCollection.add(lotesCollectionLotesToAttach);
            }
            ordenentrada.setLotesCollection(attachedLotesCollection);
            em.persist(ordenentrada);
            if (proveedoridProveedor != null) {
                proveedoridProveedor.getOrdenentradaCollection().add(ordenentrada);
                proveedoridProveedor = em.merge(proveedoridProveedor);
            }
            for (Lotes lotesCollectionLotes : ordenentrada.getLotesCollection()) {
                Ordenentrada oldOrdenEntradaidOrdenEntradaOfLotesCollectionLotes = lotesCollectionLotes.getOrdenEntradaidOrdenEntrada();
                lotesCollectionLotes.setOrdenEntradaidOrdenEntrada(ordenentrada);
                lotesCollectionLotes = em.merge(lotesCollectionLotes);
                if (oldOrdenEntradaidOrdenEntradaOfLotesCollectionLotes != null) {
                    oldOrdenEntradaidOrdenEntradaOfLotesCollectionLotes.getLotesCollection().remove(lotesCollectionLotes);
                    oldOrdenEntradaidOrdenEntradaOfLotesCollectionLotes = em.merge(oldOrdenEntradaidOrdenEntradaOfLotesCollectionLotes);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Ordenentrada ordenentrada) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Ordenentrada persistentOrdenentrada = em.find(Ordenentrada.class, ordenentrada.getIdOrdenEntrada());
            Proveedores proveedoridProveedorOld = persistentOrdenentrada.getProveedoridProveedor();
            Proveedores proveedoridProveedorNew = ordenentrada.getProveedoridProveedor();
            Collection<Lotes> lotesCollectionOld = persistentOrdenentrada.getLotesCollection();
            Collection<Lotes> lotesCollectionNew = ordenentrada.getLotesCollection();
            List<String> illegalOrphanMessages = null;
            for (Lotes lotesCollectionOldLotes : lotesCollectionOld) {
                if (!lotesCollectionNew.contains(lotesCollectionOldLotes)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Lotes " + lotesCollectionOldLotes + " since its ordenEntradaidOrdenEntrada field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (proveedoridProveedorNew != null) {
                proveedoridProveedorNew = em.getReference(proveedoridProveedorNew.getClass(), proveedoridProveedorNew.getIdProveedores());
                ordenentrada.setProveedoridProveedor(proveedoridProveedorNew);
            }
            Collection<Lotes> attachedLotesCollectionNew = new ArrayList<Lotes>();
            for (Lotes lotesCollectionNewLotesToAttach : lotesCollectionNew) {
                lotesCollectionNewLotesToAttach = em.getReference(lotesCollectionNewLotesToAttach.getClass(), lotesCollectionNewLotesToAttach.getIdLotes());
                attachedLotesCollectionNew.add(lotesCollectionNewLotesToAttach);
            }
            lotesCollectionNew = attachedLotesCollectionNew;
            ordenentrada.setLotesCollection(lotesCollectionNew);
            ordenentrada = em.merge(ordenentrada);
            if (proveedoridProveedorOld != null && !proveedoridProveedorOld.equals(proveedoridProveedorNew)) {
                proveedoridProveedorOld.getOrdenentradaCollection().remove(ordenentrada);
                proveedoridProveedorOld = em.merge(proveedoridProveedorOld);
            }
            if (proveedoridProveedorNew != null && !proveedoridProveedorNew.equals(proveedoridProveedorOld)) {
                proveedoridProveedorNew.getOrdenentradaCollection().add(ordenentrada);
                proveedoridProveedorNew = em.merge(proveedoridProveedorNew);
            }
            for (Lotes lotesCollectionNewLotes : lotesCollectionNew) {
                if (!lotesCollectionOld.contains(lotesCollectionNewLotes)) {
                    Ordenentrada oldOrdenEntradaidOrdenEntradaOfLotesCollectionNewLotes = lotesCollectionNewLotes.getOrdenEntradaidOrdenEntrada();
                    lotesCollectionNewLotes.setOrdenEntradaidOrdenEntrada(ordenentrada);
                    lotesCollectionNewLotes = em.merge(lotesCollectionNewLotes);
                    if (oldOrdenEntradaidOrdenEntradaOfLotesCollectionNewLotes != null && !oldOrdenEntradaidOrdenEntradaOfLotesCollectionNewLotes.equals(ordenentrada)) {
                        oldOrdenEntradaidOrdenEntradaOfLotesCollectionNewLotes.getLotesCollection().remove(lotesCollectionNewLotes);
                        oldOrdenEntradaidOrdenEntradaOfLotesCollectionNewLotes = em.merge(oldOrdenEntradaidOrdenEntradaOfLotesCollectionNewLotes);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = ordenentrada.getIdOrdenEntrada();
                if (findOrdenentrada(id) == null) {
                    throw new NonexistentEntityException("The ordenentrada with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Ordenentrada ordenentrada;
            try {
                ordenentrada = em.getReference(Ordenentrada.class, id);
                ordenentrada.getIdOrdenEntrada();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The ordenentrada with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Lotes> lotesCollectionOrphanCheck = ordenentrada.getLotesCollection();
            for (Lotes lotesCollectionOrphanCheckLotes : lotesCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Ordenentrada (" + ordenentrada + ") cannot be destroyed since the Lotes " + lotesCollectionOrphanCheckLotes + " in its lotesCollection field has a non-nullable ordenEntradaidOrdenEntrada field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Proveedores proveedoridProveedor = ordenentrada.getProveedoridProveedor();
            if (proveedoridProveedor != null) {
                proveedoridProveedor.getOrdenentradaCollection().remove(ordenentrada);
                proveedoridProveedor = em.merge(proveedoridProveedor);
            }
            em.remove(ordenentrada);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Ordenentrada> findOrdenentradaEntities() {
        return findOrdenentradaEntities(true, -1, -1);
    }

    public List<Ordenentrada> findOrdenentradaEntities(int maxResults, int firstResult) {
        return findOrdenentradaEntities(false, maxResults, firstResult);
    }

    private List<Ordenentrada> findOrdenentradaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Ordenentrada.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Ordenentrada findOrdenentrada(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Ordenentrada.class, id);
        } finally {
            em.close();
        }
    }

    public int getOrdenentradaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Ordenentrada> rt = cq.from(Ordenentrada.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
